#include<stdio.h>
#include<stdlib.h>

typedef struct {
	int id;
	double grade;
	char* name;
}Student;
Student* createStudent(int id, double grade, const char* name) {
	// 分配Student结构体的内存  
	Student* student = (Student*)malloc(sizeof(Student));
	if (student == NULL) {
		// 处理内存分配失败的情况  
		return NULL;
	}
	student->name = (char*)malloc(strlen(name) + 1);
	if (student->name == NULL) {
		// 释放之前为Student结构体分配的内存  
		free(student);
		// 处理内存分配失败的情况  
		return NULL;
	}

	// 复制字符串  
	strcpy(student->name, name);

	// 设置其他字段  
	student->id = id;
	student->grade = grade;

	return student;
}

typedef Student SLDataType;

typedef struct SeqList {
	SLDataType* a;
	int size;
	int capacity;
}SL;
void SeqListCheckCapacity(SL* sl) {
	//当没有空间时
	if (sl->size == sl->capacity) {
		int newcapacity = sl->capacity == 0 ? 4 : sl->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(sl->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL) {
			printf("realloc fail\n");
			exit(-1);
		}
		sl->a = tmp;
		sl->capacity = newcapacity;
	}
}
//初始化
void SeqListInit(SL* sl) {
	sl->a = NULL;
	sl->size = sl->capacity = 0;
}
//添加功能
void SeqListAdd(SL* sl, SLDataType x) {
	SeqListCheckCapacity(sl);
	sl->a[sl->size] = x;
	sl->size++;
}
//删除功能
void SeqListDelete(SL* sl) {
	if (sl->size > 0) {
		sl->size--;
	}
	else
		printf("顺序表中已经没有数据，删除失败");
}
void SeqListPrint(SL* sl) {
	if (sl != NULL) {
		// 使用Student实例  
		for (int i = 0; i < sl->size; i++) {
			printf("ID: %d, Grade: %.2f, Name: %s\n", sl->a[i].id, sl->a[i].grade, sl->a[i].name);
		}
	}
}
void SeqListMerge(SL* sl1, SL* sl2) {
	if (sl1->capacity < sl1->size + sl2->size) {
		int newcapacity = sl1->capacity + sl2->capacity;
		SLDataType* tmp = (SLDataType*)realloc(sl1->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL) {
			printf("realloc fail\n");
			exit(-1);
		}
		sl1->a = tmp;
		sl1->capacity = newcapacity;
	}
	for (int i = 0; i < sl2->size; i++) {
		sl1->a[sl1->size] = sl2->a[i];
		sl1->size++;
	}
}
void menu() {
	printf("------------------------\n");
	printf("-----欢迎使用本系统-----\n");
	printf("--------请选择：--------\n");
	printf("------1.插入数据：------\n");
	printf("------2.删除数据：------\n");
	printf("------3.合并数据：------\n");
	printf("------4.查看数据：------\n");
	printf("------------------------\n");
}

int main() {
	SL sl;
	SL newsl;
	SeqListInit(&sl);
	SeqListInit(&newsl);
	Student* stu1 = createStudent(1, 90.1, "大娃");
	Student* stu2 = createStudent(2, 80, "二娃");
	Student* stu3 = createStudent(3, 100, "三娃");
	Student* stu4 = createStudent(4, 6, "四娃");
	Student* stu5 = createStudent(5, 88.8, "五娃");
	Student* stu6 = createStudent(6, 0, "六娃");
	Student* stu7 = createStudent(7, 75, "七娃");

	SeqListAdd(&sl, *stu1);
	SeqListAdd(&sl, *stu3);
	SeqListAdd(&sl, *stu4);
	SeqListAdd(&sl, *stu5);
	SeqListAdd(&sl, *stu6);
	SeqListAdd(&sl, *stu7);
	menu();
	int choice;
	scanf_s("%d", &choice);
	switch (choice) {
	case 1:
	{
		int id,input;
		double grade;
		char name[20];
		printf("请输入学生学号:");
		scanf_s("%d", &id);
		printf("请输入学生分数:");
		scanf_s("%lf", &grade);
		printf("请输入学生姓名:");
		scanf_s("%19s", name,sizeof(name));
		Student* stus = createStudent(id, grade, name);
		SeqListAdd(&newsl, *stus);
		printf("输入“1”可输入更多学生信息");
		scanf_s("%d", &input);
		if (input != 1) {
			break;
		}
	case 2: {
		int choice;
		printf("请问您要删除 1.原有表 还是 2.新添加的表（请输入数字 1 或 2）的末尾");
		scanf_s("%d", &choice);
		switch (choice) {
		case 1: {
			SeqListDelete(&sl);
			printf("删除完成");
		}
		case 2:
		{
			SeqListDelete(&newsl);
			printf("删除完成");
		}
		break;
		}
	}
	case 3:
	{
		SeqListMerge(&sl, &newsl);
		printf("完成与已有表合并");
	}
	}
	case 4: {
		int input;
		printf("请问您要查看数据 1.原有表 还是 2.新添加的表：（请输入数字 1 或 2）");
		scanf_s("%d", &input);
		switch (input) {
		case 1: {
			if (sl.size == sl.capacity) {
				printf("表中无数据");
			}
			else {
				SeqListPrint(&sl);
			}
		}
		case 2: {
			if (newsl.size == newsl.capacity) {
				printf("新表没有数据，可能是已合并入原有表");
			}
			else {
				SeqListPrint(&newsl);
			}
		}
		}
		break;
	}

	}

}